# 实验1：使用unittest框架注册接口测试
import csv

import requests
import unittest


# class RegisterTest(unittest.TestCase):
#     def setUp(self):
#         self.url = "http://129.211.129.101:9006/user/register.do"
#         self.user_info = {
#             "username": "lllljl",
#             "password": "123456",
#             "email": "lllljjj@qq.com",
#             "phone": "13555555556",
#             "question": "最喜欢的书",
#             "answer": "阿衰"
#         }
#
#     def test_register(self):
#         s = requests.session()
#         res = s.post(self.url, data=self.user_info).json()
#         self.assertIn("用户名已经存在", str(res))
#
#
# if __name__ == '__main__':
#     unittest.main()

# 实验2：针对检查接口通过unittest框架进行脚本实现
# class CheckTest(unittest.TestCase):
#     def setUp(self):
#         self.url = "http://129.211.129.101:9006/user/check_vaild.do"
#         self.file1 = open("../data/register_data.csv", "r")
#         self.file2 = open("../data/register_res.csv", "w")
#
#     def test_check(self):
#         table = csv.reader(self.file1)
#         check_info = {}
#         for row in table:
#             check_info["str"] = row[0]
#             check_info["type"] = row[1]
#             s = requests.session()
#             res = s.post(self.url, data=check_info).text
#             self.assertIn(row[2], str(res))
#
#     def tearDown(self):
#         self.file1.close()
#         self.file2.close()
#
#
# if __name__ == '__main__':
#     unittest.main()

# 实验3：执行顺序
class DemoTest(unittest.TestCase):
    def setUp(self):
        print("setup")

    def tearDown(self):
        print("teardown")

    def test_case1(self):
        print("case1")

    def test_case2(self):
        print("case2")

    def test_case3(self):
        print("case3")


if __name__ == '__main__':
    # # 声明测试套对象
    # suite = unittest.TestSuite()
    # suite.addTest(DemoTest("test_case2"))
    # # 声明测试运行对象
    # runner = unittest.TextTestRunner()
    # runner.run(suite)

    # 以文件方式执行
    # 声明文件所在路径
    testdir = './'
    discover = unittest.defaultTestLoader.discover(testdir, pattern='test_*.py')
    # 声明测试运行对象
    runner = unittest.TextTestRunner()
    runner.run(discover)
